package com.ess.project.examine.record.controller;

import com.ess.framework.aspectj.lang.annotation.Log;
import com.ess.framework.aspectj.lang.enums.BusinessType;
import com.ess.framework.web.controller.BaseController;
import com.ess.framework.web.domain.AjaxResult;
import com.ess.framework.web.page.TableDataInfo;
import com.ess.project.examine.grade.domain.ExamineGradeEntity;
import com.ess.project.examine.grade.service.IGradeService;
import com.ess.project.examine.record.domain.ExamineRecordEntity;
import com.ess.project.examine.record.service.IRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * 考核记录管理
 *
 * @author daihui
 */
@Controller
@RequestMapping("/examine/record")
public class RecordController extends BaseController {
    private String prefix = "examine/record";

    @Autowired
    private IRecordService recordService;
    @Autowired
    private IGradeService gradeService;



    @RequiresPermissions("examine:record:view")
    @GetMapping()
    public String record() {
        return  prefix + "/record";
    }

    @RequiresPermissions("examine:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamineRecordEntity record) {
        startPage();
        List<ExamineRecordEntity> list = recordService.selectRecordList(record);
        return getDataTable(list);
    }

    @PostMapping("/getRecords")
    @ResponseBody
    public List<ExamineRecordEntity> getRecords(ExamineRecordEntity record) {
        List<ExamineRecordEntity> list = recordService.selectRecordList(record);
        return list;
    }

    @Log(title = "考核记录管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("examine:record:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        InputStream inputStream = file.getInputStream();
        List<ExamineGradeEntity> gradeList = recordService.importRecordData(inputStream);
        boolean saveBatch = gradeService.saveBatch(gradeList);
        if(saveBatch){
            return AjaxResult.success("导入成功");
        }
        return AjaxResult.error();
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Integer recordId, ModelMap mmap) {
        ExamineRecordEntity record = recordService.selectRecordById(recordId);
        mmap.put("record", record);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("examine:record:edit")
    @Log(title = "考核记录管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ExamineRecordEntity record) {
        return toAjax(recordService.updateRecord(record));
    }

    @RequiresPermissions("examine:record:remove")
    @Log(title = "考核记录管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(recordService.deleteRecordByIds(ids));
    }

//    @Log(title = "考核人员管理", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("examine:record:export")
//    @GetMapping("/export")
//    @ResponseBody
//    public void export(ExaminePersonnelEntity record, HttpServletResponse response) {
//        recordService.export(record,response);
//    }


//    @RequiresPermissions("examine:record:view")
//    @GetMapping("/importTemplate")
//    @ResponseBody
//    public AjaxResult importTemplate() {
//        ExcelUtil<ExaminePersonnelEntity> util = new ExcelUtil<>(ExaminePersonnelEntity.class);
//        return util.importTemplateExcel("考核人员数据");
//    }


//    /**
//     * 新增保存用户
//     */
//    @RequiresPermissions("examine:record:add")
//    @Log(title = "考核人员管理", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(@Validated ExaminePersonnelEntity record) {
//        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(recordService.checkPersonnelNameUnique(record.getExamPersonnel()))) {
//            return error("新增考核人员'" + record.getExamPersonnel() + "'失败，改考核人员已存在");
//        }
//        return toAjax(recordService);
//    }
//
//
//    /**
//     * 修改保存用户
//     */
//    @RequiresPermissions("examine:record:edit")
//    @Log(title = "考核人员管理", businessType = BusinessType.UPDATE)
//    @PostMapping("/edit")
//    @ResponseBody
//    public AjaxResult editSave(@Validated ExaminePersonnelEntity record) {
//        return toAjax(recordService.updatePersonnel(record));
//    }
//
//
//    @RequiresPermissions("examine:record:remove")
//    @Log(title = "考核人员管理", businessType = BusinessType.DELETE)
//    @PostMapping("/remove")
//    @ResponseBody
//    public AjaxResult remove(String ids) {
//        return toAjax(recordService.deletePersonnelByIds(ids));
//    }


}
